<?php

namespace spcj;

use think\db\exception\DbException;

class LpLog
{
    /**
     * 运行
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function run()
    {
        // 每次处理最近三天
        for ($i = 0;$i>=-5;$i--)
        {
            $id1 = [];
            $id0 = [];
            $cdate = date('ymd', strtotime($i . ' day'));

            $sql = "select * from (
select id,ip,lp_type,shop_id,DATE_FORMAT(us_at, '%y%m%d') AS cdate from ddhd_sp_ip_logs 
) as a where cdate=".$cdate;

            $query = DbConn::getConn()->query($sql);
            if ($query){
                foreach ($query as $row){
                    $key = $row['shop_id'].'-'.$row['lp_type'];

                    // 如果存在，且数组里有该ip，则处理
                    if (isset($qc_arr[$key]) && (in_array($row['ip'],$qc_arr[$key]))){
                        $id0[] = $row['id'];
                    }else{
                        $qc_arr[$key][] = $row['ip'];
                        $id1[] = $row['id'];
                    }
                }



                if ($id0){
                    DbConn::getConn()->table('ddhd_sp_ip_logs')->where('id','in',$id0)->update(['is_deduplicated' => 0]);
                }

                if ($id1){
                    DbConn::getConn()->table('ddhd_sp_ip_logs')->where('id','in',$id1)->update(['is_deduplicated' => 1]);
                }
            }

        }

    }
}
